package com.util;

import com.RSA.RSAKeyPair;
import com.RSA.Safety;
import com.generator.entity.Reply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * @Author: htj
 * @Date: 2021/10/2 0002 上午 0:22
 * 统一模拟服务模块
 */
@Component
public class MockService {

    @Autowired
    private Safety safety;

    public Reply service(int seqNo, int api_id, int thirdParty_id, String ciphertext, RSAKeyPair keyPair) throws Exception {

        //模拟私钥解密
        String content1 = safety.decryptByPrivateKey(keyPair.getPrivateKey(), ciphertext);//解密后的请求明文
        System.out.println("私钥解密后：" + content1);

        //模拟私钥加密
        String ciphertext2 = safety.encryptByPrivateKey(keyPair.getPrivateKey(), "返回报文内容：XXXXX");
        System.out.println("私钥加密后：" + ciphertext2);

        //模拟公钥解密
        String content2 = safety.decryptByPublicKey(keyPair.getPublicKey(), ciphertext2);//明文
        System.out.println("公钥解密后：" + content2);

        //返回内容
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Reply result = new Reply();
        result.setId(seqNo);
        result.setThirdpartyId(thirdParty_id);
        result.setApiId(api_id);
        result.setStatus(0);
        result.setMsg("返回成功");
        result.setContent(content2);
        result.setReturnTime(timestamp);

        try {
            Thread.sleep(190);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return result;
    }

}
